<?php
class ServiceController extends CController {

	const PAGE_SIZE = 10;

	public function getSubmenu()
	{
		$all = array(
				'create' => array(
						'icon' => Yii::app()->request->baseUrl . '/images/add.png',
						'label' => Yii::t('submenu', 'New service'),
						'href' => array('create')
					),
				'admin' => array(
						'icon' => Yii::app()->request->baseUrl . '/images/list.png',
						'label' => Yii::t('submenu', 'Manage services'),
						'href' => array('admin')
					)
			);

		$admin = array();
		$root = array();
		if ($this->_model != null) {
			$admin = array(
				'update' => array(
					'icon' => Yii::app()->request->baseUrl . '/images/edit.png',
					'label' => Yii::t('submenu', 'Edit service'),
					'href' => array ('update','id' => $this->_model->id)
				)
			);

			$root = array(
				'delete' => array(
					'icon' => Yii::app()->request->baseUrl . '/images/del.png',
					'label' => Yii::t('submenu', 'Delete service'),
					'link' => array (
						'submit' => array ('delete','id' => $this->_model->id),
						'confirm' => Yii::t('submenu', 'Are you sure?')
					)
				)
			);
		}

		$submenu = array_merge($all, $admin);

//		if ($this->_model->username != Constants::ADMIN_USER && $this->_model->username != Constants::ROOT_USER) {
			$submenu = array_merge($submenu, $root);
//		}

		return $submenu;
	}

	/**
	 * @var string specifies the default action to be 'list'.
	 */
	public $defaultAction = 'admin';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters() {
		return array (
			'accessControl', // perform access control for CRUD operations

		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
				return array (
			array(
				'allow', 
				'actions' => array(
					'admin', 'show'
				),
				'roles' => array('Services.canList')
			),
			array(
				'allow', 
				'actions' => array(
					'update'
				),
				'roles' => array('Services.canUpdate')
			),
			array(
				'allow', 
				'actions' => array(
					'create'
				),
				'roles' => array('Services.canCreate')
			),
			array(
				'allow', 
				'actions' => array(
					'delete'
				),
				'roles' => array('Services.canDelete')
			),
			array (
				'deny', // deny all users
				'users' => array (
					'*'
				)
			)
		);
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'show' page.
	 */
	public function actionCreate() {
		$model = new Service();
		if (isset ($_POST['Service'])) {
			$model->attributes = $_POST['Service'];
			if ($model->save())
				$this->redirect(array (
					'admin'
				));
		}
		else if (isset ($_POST['service'])) {
			$model->attributes = $_POST['service'];
			if ($model->save())
				$this->redirect(array (
					'admin'
				));
		}
		$this->render('create', array (
			'model' => $model
		));
//		Constants::print_var($model);
//		Constants::print_var($_POST['Service']);
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'show' page.
	 */
	public function actionUpdate() {
		$model = $this->loadservice();
		if (isset ($_POST['Service'])) {
			$model->attributes = $_POST['Service'];
			if ($model->save())
				$this->redirect(array (
					'admin'
				));
		} else if (isset ($_POST['service'])) {
			$model->attributes = $_POST['service'];
			if ($model->save())
				$this->redirect(array (
					'admin'
				));
		}
		$this->render('update', array (
			'model' => $model
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'list' page.
	 */
	public function actionDelete() {
		if (Yii::app()->request->isPostRequest) {
			// we only allow deletion via POST request
			$this->loadservice()->delete();
			$this->redirect(array (
				'list'
			));
		} else
			throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin() {
		$this->processAdminCommand();

		$criteria = new CDbCriteria;

		$pages = new CPagination(Service::model()->count($criteria));
		$pages->pageSize = self::PAGE_SIZE;
		$pages->applyLimit($criteria);

		$sort = new CSort('service');
		$sort->applyOrder($criteria);

		$models = Service::model()->findAll($criteria);

		$this->render('admin', array (
			'models' => $models,
			'pages' => $pages,
			'sort' => $sort,

		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
	 */
	public function loadservice($id = null) {
		if ($this->_model === null) {
			if ($id !== null || isset ($_GET['id']))
				$this->_model = Service::model()->findbyPk($id !== null ? $id : $_GET['id']);
			if ($this->_model === null)
				throw new CHttpException(404, 'The requested page does not exist.');
		}
		return $this->_model;
	}

	/**
	 * Executes any command triggered on the admin page.
	 */
	protected function processAdminCommand() {
		if (isset ($_POST['command'], $_POST['id']) && $_POST['command'] === 'delete') {
			$this->loadservice($_POST['id'])->delete();
			// reload the current page to avoid duplicated delete actions
			$this->refresh();
		}
	}
}
?>
